<template>
  <div>
    <slot></slot>
  </div>
</template>

<script>
export default {
  provide() {
    return {
      form: this,
    }
  },
  props: {
    model: { type: Object, required: true },
    rules: { type: Object },
  },
  created() {
    this.$on('validate', this.validate)
  },
  methods: {
    async validate(cb) {
      // 全局校验
      const tasks = this.$children
        .filter((item) => item.prop)
        .map((item) => item.validate())
      const results = await Promise.all(tasks)
      if (results.some((valid) => !valid)) {
        // 校验失败
        cb(false)
      } else {
        cb(true)
      }
    },
  },
}
</script>

<style scoped></style>
